import 'package:casdon/modules/recipe/data/api/miffy_recipe_info_api.dart';
import 'package:casdon/modules/recipe/data/model/recipe_ecoprotocol_model.dart';
import 'package:tuya_home_sdk_flutter/tuya_home_sdk_flutter.dart';

class MiffyRecipeProtocolModel extends RecipeEcoprotocol {
  @override
  late final List<Map<String, String>> cookParams;
  @override
  late final Map<String, dynamic> dps;

  MiffyRecipeProtocolModel();

  // 静态方法转换
  static MiffyRecipeProtocolModel? fromApi(
    ResponseMiffyRecipeInfoApi? response, {
    required ThingSmartDeviceModel devModel,
  }) {
    if (response == null) return null;
    final stepInfos = response.cookStepInfoVOList;
    if (stepInfos.isEmpty) return null;
    List<Map<String, String>> cookParams = [];
    Map<String, dynamic> dps = {};

    String allModeKey =
        '${getDpidWithDpCode(dpCode: "all_mode", devModel: devModel)}';
    String switchKey =
        '${getDpidWithDpCode(dpCode: "switch", devModel: devModel)}';
    String startKey =
        '${getDpidWithDpCode(dpCode: "start", devModel: devModel)}';
    String diyModeKey =
        '${getDpidWithDpCode(dpCode: "diy_mode", devModel: devModel)}';

    dps.addEntries([MapEntry(switchKey, true)]); // switch: 电源
    dps.addEntries([MapEntry(startKey, true)]); // start： 开始
    if (getDpTypeWithDpCode(dpCode: "all_mode", devModel: devModel) ==
        "value") {
      dps.addEntries([MapEntry(allModeKey, 20)]);
    } else {
      dps.addEntries([MapEntry(allModeKey, "20")]);
    }

    var ecoProto = "0000";
    ecoProto =
        '$ecoProto${1.toRadixString(16).padLeft(2, '0').toUpperCase()}'; // 步骤总数，16进制
    //CookStepInfo stepInfo in stepInfos
    for (int step = stepInfos.length - 1; step < stepInfos.length; ++step) {
      ecoProto =
          '$ecoProto${1.toRadixString(16).padLeft(2, '0').toUpperCase()}'; // 第几步骤，16进制
      for (final stepPara in stepInfos[step].stepParaVos) {
        cookParams.add({
          stepPara.functionPointName!: '${stepPara.dbValue}${stepPara.numUnit}',
        });
      }
      for (final stepPara in stepInfos[step].stepParaVos) {
        if (stepPara.dpCode == "all_mode") {
          if (getDpTypeWithDpCode(dpCode: "all_mode", devModel: devModel) ==
              "value") {
            dps.addEntries([MapEntry(allModeKey, 20)]);
          } else {
            dps.addEntries([MapEntry(allModeKey, "20")]);
          }
          ecoProto =
              '$ecoProto${int.parse(stepPara.dbValue!).toRadixString(16).padLeft(2, '0').toUpperCase()}'; // 模式
        }
      }
      for (final stepPara in stepInfos[step].stepParaVos) {
        if (stepPara.dpCode == "cook_temperature") {
          ecoProto =
              '$ecoProto${int.parse(stepPara.dbValue!).toRadixString(16).padLeft(4, '0').toUpperCase()}'; // 上管温度
          ecoProto =
              '$ecoProto${0.toRadixString(16).padLeft(4, '0').toUpperCase()}'; // 上管温度
        }
      }
      for (final stepPara in stepInfos[step].stepParaVos) {
        if (stepPara.dpCode == "cook_time") {
          final time = int.parse(stepPara.dbValue!);
          int min = time % 60;
          int hour = (time / 60).floor();
          ecoProto =
              '$ecoProto${hour.toRadixString(16).padLeft(2, '0').toUpperCase()}'; // 时间： 时
          ecoProto =
              '$ecoProto${min.toRadixString(16).padLeft(2, '0').toUpperCase()}'; // 时间： 分
        }
      }
    }
    dps.addEntries([MapEntry(diyModeKey, ecoProto)]);
    MiffyRecipeProtocolModel model = MiffyRecipeProtocolModel();
    model.dps = dps;
    model.cookParams = cookParams;
    return model;
  }

  static int getDpidWithDpCode({
    required String dpCode,
    required ThingSmartDeviceModel devModel,
  }) {
    for (var schemaModel in devModel.schemaArr!) {
      if (dpCode == schemaModel.code) {
        return schemaModel.id;
      }
    }
    return 0;
  }

  static String? getDpTypeWithDpCode({
    required String dpCode,
    required ThingSmartDeviceModel devModel,
  }) {
    if (devModel.schemaArr == null) return null;
    for (var schemaModel in devModel.schemaArr!) {
      if (dpCode == schemaModel.code) {
        return propertyTypeValues.reverse[schemaModel.property!.type];
      }
    }
    return null;
  }
}
